package com.aiyun.research.springcloudalibabademo.config;

import java.io.PrintWriter;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SentinelWebConfig {

	@Bean
	public BlockExceptionHandler sentinelBlockExceptionHandler() {
		return (request, response, e) -> {
			// 429 Too Many Requests
			response.setStatus(429);

			PrintWriter out = response.getWriter();
			out.print("Oops, blocked by Sentinel: " + e.getClass().getSimpleName());
			out.flush();
			out.close();
		};
	}
}
